package ray.create;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.ArrayList;
import java.util.List;

/**
 * generate service class
 *
 * @author ray
 * @date 2016-03-17 10:34:45
 */
public class MybatisServiceBasePlugin extends PluginAdapter {

    private FullyQualifiedJavaType daoType;
    private FullyQualifiedJavaType pojoType;
    private FullyQualifiedJavaType interfaceType;
    private FullyQualifiedJavaType commonInterfaceType;

    private String servicePack;
    private String interfaceClass;
    private String project;
    private String pojoUrl;

    /**
     * 是否添加注解
     */
    private boolean enableAnnotation = true;
    private boolean enableFunction = false;

    public MybatisServiceBasePlugin() {
        super();
    }

    /**
     * 读取配置文件
     */
    @Override
    public boolean validate(List<String> warnings) {

        String enableAnnotation = properties.getProperty("enableAnnotation");

        String enableFunction = properties.getProperty("enableFunction");


        if (StringUtility.stringHasValue(enableAnnotation)) {
            this.enableAnnotation = StringUtility.isTrue(enableAnnotation);
        }

        if (StringUtility.stringHasValue(enableFunction)) {
            this.enableFunction = StringUtility.isTrue(enableFunction);
        }


        this.interfaceClass = properties.getProperty("interfaceClass");
        this.servicePack = properties.getProperty("targetPackage");
        this.project = properties.getProperty("targetProject");
        this.pojoUrl = context.getJavaModelGeneratorConfiguration().getTargetPackage();

        return true;
    }


    /**
     *
     */
    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {


        String table = introspectedTable.getBaseRecordType();
        String tableName = table.replaceAll(this.pojoUrl + ".", "");

        List<GeneratedJavaFile> files = new ArrayList<GeneratedJavaFile>();

        commonInterfaceType = new FullyQualifiedJavaType(interfaceClass + " <" + tableName + ">");

        interfaceType = new FullyQualifiedJavaType(servicePack + "." + tableName + "Service");

        daoType = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());

        pojoType = new FullyQualifiedJavaType(pojoUrl + "." + tableName);

        Interface interfaces = new Interface(interfaceType);
        interfaces.addSuperInterface(new FullyQualifiedJavaType("BaseSupportService" + " <" + tableName + ">"));

        TopLevelClass topLevelClass = new TopLevelClass(interfaceType);

        // 导入必须的类
        addImport(interfaces, topLevelClass);

        // 接口
        addService(interfaces, introspectedTable, "Entity", files);

        addLogger(topLevelClass);

        return files;
    }

    /**
     * add interface
     *
     * @param tableName
     * @param files
     */
    protected void addService(Interface interface1, IntrospectedTable introspectedTable, String tableName, List<GeneratedJavaFile> files) {

        interface1.setVisibility(JavaVisibility.PUBLIC);

        interface1.addJavaDocLine("/**");
        interface1.addJavaDocLine(" *");
        interface1.addJavaDocLine(" *@author create by Generator ");
        interface1.addJavaDocLine(" **/");
        GeneratedJavaFile file = new GeneratedJavaFile(interface1, project, context.getJavaFormatter());

        files.add(file);
    }


    /**
     * 添加字段
     *
     * @param topLevelClass
     */
    protected void addField(TopLevelClass topLevelClass, String tableName) {
        // add dao
        Field field = new Field();
        // set var name
        field.setName(Utils.toLowerCase(daoType.getShortName()));
        topLevelClass.addImportedType(daoType);
        field.setType(daoType); // type
        field.setVisibility(JavaVisibility.PRIVATE);
        if (enableAnnotation) {
            field.addAnnotation("@Resource");
        }
        topLevelClass.addField(field);
    }


    /**
     * import must class
     */
    private void addImport(Interface interfaces, TopLevelClass topLevelClass) {
        //interfaces.addImportedType(new FullyQualifiedJavaType("com.zjhc.flyhong.common.function.operator.LoginUser"));
        //interfaces.addImportedType(new FullyQualifiedJavaType("com.zjhc.flyhong.common.web.Result"));
        //interfaces.addImportedType(new FullyQualifiedJavaType("com.zjhc.flyhong.common.function.operator.PageDTO"));
        interfaces.addImportedType(pojoType);
        interfaces.addImportedType(commonInterfaceType);

    }

    /**
     * import logger
     */
    private void addLogger(TopLevelClass topLevelClass) {
        Field field = new Field();
        field.setFinal(true);
        field.setInitializationString("LoggerFactory.getLogger(" + topLevelClass.getType().getShortName() + ".class)"); // set value
        field.setName("logger"); // set var name
        field.setStatic(true);
        field.setType(new FullyQualifiedJavaType("Logger")); // type
        field.setVisibility(JavaVisibility.PRIVATE);
        topLevelClass.addField(field);
    }


    @Override
    public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        //returnType = method.getReturnType();
        return true;
    }
}
